package org.abratuhi.kvngroupware.gui;

import java.awt.Graphics;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.border.TitledBorder;

import org.abratuhi.kvngroupware.client.KVNGroupwareClient;
import org.abratuhi.kvngroupware.controller.MainController;
import org.abratuhi.kvngroupware.spring.SpringUtilities;

@SuppressWarnings("serial")
public class GUIMain extends JPanel{
	
	KVNGroupwareClient kvnclient;
	
	MainController controller;
	
	public JButton buttonJoinChatroom = new JButton();
	public JComboBox comboChatrooms = new JComboBox();
	public JButton buttonCreateChatroom = new JButton();
	public JTextField fieldChatroom = new JTextField();
	
	public JButton buttonJoinBrainstorm = new JButton();
	public JComboBox comboBrainstorms = new JComboBox();
	public JButton buttonCreateBrainstorm = new JButton();
	public JTextField fieldBrainstorm = new JTextField();
	
	public JButton buttonEditScenario = new JButton();
	public JComboBox comboScenarios = new JComboBox();
	public JButton buttonCreateScenario = new JButton();
	public JTextField fieldScenario = new JTextField();
	
	public JButton buttonJokesManager = new JButton();
	
	public int indexSelectedChatroom = -1;
	public int indexSelectedBrainstorm = -1;
	public int indexSelectedScenario = -1;
	
	//JTextArea textareaLog = new JTextArea();
	
	JFrame frame;
	
	public GUIMain(KVNGroupwareClient kvnclient, Properties locale){
		super();
		
		this.buttonJoinChatroom.setText(locale.getProperty("main.button.chatroom.join"));
		this.buttonCreateChatroom.setText(locale.getProperty("main.button.chatroom.create"));
		this.buttonCreateBrainstorm.setText(locale.getProperty("main.button.brainstorm.create"));
		this.buttonJoinBrainstorm.setText(locale.getProperty("main.button.brainstorm.join"));
		this.buttonCreateScenario.setText(locale.getProperty("main.button.scenario.create"));
		this.buttonEditScenario.setText(locale.getProperty("main.button.scenario.edit"));
		this.buttonJokesManager.setText(locale.getProperty("main.button.jokesmanager"));
		
		this.kvnclient = kvnclient;
		this.controller = new MainController(this.kvnclient, this);
		
		this.buttonCreateBrainstorm.addActionListener(this.controller);
		this.buttonCreateChatroom.addActionListener(this.controller);
		this.buttonEditScenario.addActionListener(controller);
		this.buttonJoinBrainstorm.addActionListener(this.controller);
		this.buttonJoinChatroom.addActionListener(this.controller);
		this.buttonCreateScenario.addActionListener(controller);
		this.buttonJokesManager.addActionListener(controller);
		
		this.comboBrainstorms.addItemListener(controller);
		this.comboChatrooms.addItemListener(this.controller);
		comboScenarios.addItemListener(controller);
		
		JPanel panelChatroom = new JPanel();
		panelChatroom.setLayout(new SpringLayout());
		panelChatroom.setBorder(new TitledBorder(locale.getProperty("main.border.chatrooms")));
		panelChatroom.add(comboChatrooms);
		panelChatroom.add(buttonJoinChatroom);
		panelChatroom.add(fieldChatroom);
		panelChatroom.add(buttonCreateChatroom);
		SpringUtilities.makeCompactGrid(panelChatroom, 2, 2, 0, 0, 0, 0);
		
		JPanel panelBrainstorm = new JPanel();
		panelBrainstorm.setLayout(new SpringLayout());
		panelBrainstorm.setBorder(new TitledBorder(locale.getProperty("main.border.brainstorms")));
		panelBrainstorm.add(comboBrainstorms);
		panelBrainstorm.add(buttonJoinBrainstorm);
		panelBrainstorm.add(fieldBrainstorm);
		panelBrainstorm.add(buttonCreateBrainstorm);
		SpringUtilities.makeCompactGrid(panelBrainstorm, 2, 2, 0, 0, 0, 0);
		
		JPanel panelScenario = new JPanel();
		panelScenario.setLayout(new SpringLayout());
		panelScenario.setBorder(new TitledBorder(locale.getProperty("main.border.scenarios")));
		panelScenario.add(comboScenarios);
		panelScenario.add(buttonEditScenario);
		panelScenario.add(fieldScenario);
		panelScenario.add(buttonCreateScenario);
		SpringUtilities.makeCompactGrid(panelScenario, 2, 2, 0, 0, 0, 0);
		
		JPanel panelJokesManager = new JPanel();
		panelJokesManager.setLayout(new SpringLayout());
		panelJokesManager.setBorder(new TitledBorder(locale.getProperty("main.border.jokesmanager")));
		panelJokesManager.add(buttonJokesManager);
		SpringUtilities.makeCompactGrid(panelJokesManager, 1, 1, 0, 0, 0, 0);
		
		setLayout(new SpringLayout());
		setVisible(true);
		add(panelChatroom);
		add(panelBrainstorm);
		add(panelScenario);
		add(panelJokesManager);
		SpringUtilities.makeCompactGrid(this, 4, 1, 0, 0, 0, 0);
		
	}
	
	//TODO: remove this ugly workaround 'add/remove jcombobox listener'
	//JComboBox fires an ItemStateChanged/ActionPerformed event twice when removing all items and then adding an item
	//possible solution: implementing own ComboBoxModel
	public void paint(Graphics g){
		super.paint(g);
		
		comboBrainstorms.removeActionListener(controller);
		comboBrainstorms.removeAllItems();
		for(int i=0; i<kvnclient.getBrainstorms().size(); i++){
			comboBrainstorms.addItem(kvnclient.getBrainstorms().get(i).getTheme());
		}
		if(indexSelectedBrainstorm!=-1)comboBrainstorms.setSelectedIndex(indexSelectedBrainstorm);
		comboBrainstorms.addActionListener(controller);
		
		comboChatrooms.removeActionListener(controller);
		String[] chatroomIds = this.kvnclient.getChatroomIdsAsArray();
		comboChatrooms.removeAllItems();
		for(int i=0; i<chatroomIds.length; i++){
			comboChatrooms.addItem(chatroomIds[i]);
		}
		if(indexSelectedChatroom!=-1)comboChatrooms.setSelectedIndex(indexSelectedChatroom);
		comboChatrooms.addActionListener(controller);
		
		comboScenarios.removeActionListener(controller);
		comboScenarios.removeAllItems();
		for(int i=0; i<kvnclient.getScenarioNumber(); i++){
			comboScenarios.addItem(kvnclient.getScenario(i).getName());
		}
		if(indexSelectedScenario!=-1)comboScenarios.setSelectedIndex(indexSelectedScenario);
		comboScenarios.addActionListener(controller);
		
		//super.paint(g);
	}
	
	/*public void update(){
		String[] brainstormIds = this.kvnclient.getBrainstormIdsAsArray();
		comboBrainstorms.removeAllItems();
		for(int i=0; i<brainstormIds.length; i++){
			comboBrainstorms.addItem(brainstormIds[i]);
		}
		
		String[] chatroomIds = this.kvnclient.getChatroomIdsAsArray();
		comboChatrooms.removeAllItems();
		for(int i=0; i<chatroomIds.length; i++){
			comboChatrooms.addItem(chatroomIds[i]);
		}
	}*/
	
	public void createJFrame(){
		frame = new JFrame("..:: KVN Groupware Client ::..");
		frame.getContentPane().add(this);
		frame.setVisible(true);
		frame.addWindowListener(this.controller);
		frame.pack();
	}
	
	public void disposeJFrame(){
		frame.dispose();
	}

}
